package week_six;

import java.util.Arrays;

public class HouseRobberII_213 {
    public int rob(int[] nums) {

        int n = nums.length;
        if (n == 1) return nums[0];
        int[] houses = new int[n + 1];
        for (int i = 1; i <= n; i++) houses[i] = nums[i - 1];

        int[][] f = new int[n + 1][2];
        // for(int i=0;i<=n;i++) {
        //     Arrays.fill(f[i], -1000000);
        // }
        f[1][0] = 0;
        f[1][1] = -1000000;


        dpCalc(n, houses, f);

        int ans = Math.max(f[n][0], f[n][1]);

        for (int i = 0; i <= n; i++) {
            Arrays.fill(f[i], -1000000);
        }
        f[1][1] = houses[1];
        // f[1][0]=-1000000;


        dpCalc(n, houses, f);

        ans = Math.max(ans, f[n][0]);
        return ans;

    }

    private void dpCalc(int n, int[] houses, int[][] f) {
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j <= 1; j++) {
                f[i][0] = Math.max(f[i - 1][1], f[i - 1][0]);
                f[i][1] = f[i - 1][0] + houses[i];
            }
        }
    }
}
